import wx
import os


class DiaPickTemp(wx.Dialog):

    def __init__(self, parent, title, *args, **kwargs):
        super().__init__(parent, -1, title)
        self.Path = kwargs.get('path')
        if self.Path is None:
            self.Path = 'c:/'
        else:
            self.Path = os.path.join(self.Path, 'Temp')
        self.WildCard = 'Excel files (*.xlsx)|*.xlsx|Excel files (*.xls)|*.xls|Word files (*.docx)|*.docx|All files(*.*)|*.*'
        MainSizer = wx.BoxSizer(wx.VERTICAL)
        BtnSizer = wx.BoxSizer(wx.HORIZONTAL)

        self.PathNames = ['模板路径', '导出路径']
        self.InitFilePicker(MainSizer, self.PathNames)
        MainSizer.Add(BtnSizer, flag=wx.ALL | wx.EXPAND, border=5)
        self.BindNames = ('self.OnSave', 'self.OnCancel')
        names = ['确定', '取消']
        self.InitBtn(BtnSizer, names)

        self.SetSizer(MainSizer)
        MainSizer.Fit(self)
        MainSizer.SetSizeHints(self)

    def InitFilePicker(self, sizer, names):
        for index, name in enumerate(names):
            Sizer = wx.StaticBoxSizer(wx.HORIZONTAL, self, name)
            if index == 0:
                DirPicker = wx.FilePickerCtrl(self, -1, self.Path, '请选择' + name,
                                              wildcard=self.WildCard, name='picker' + name, size=(500, -1))
                self.Bind(wx.EVT_FILEPICKER_CHANGED,
                          self.OnPickPathChange, DirPicker)
            else:
                DirPicker = wx.FilePickerCtrl(self, -1, self.Path, '请选择' + name,
                                              wildcard='All files(*.*)|*.*', name='picker' + name, size=(500, -1),
                                              style=wx.FLP_SAVE)
            Sizer.Add(DirPicker, flag=wx.ALL | wx.EXPAND, border=5)
            sizer.Add(Sizer, flag=wx.ALL | wx.EXPAND, border=5)

    def InitBtn(self, sizer, names):
        sizer.Add((140, 20), 1)
        for index, name in enumerate(names):
            Btn = wx.Button(self, -1, name, name='btn' + name)
            sizer.Add(Btn, flag=wx.ALL | wx.EXPAND, border=5)
            self.Bind(wx.EVT_BUTTON, eval(self.BindNames[index]), Btn)

    def OnPickPathChange(self, event):
        # Obj = self.FindWindowByName('picker' + self.PathNames[0])
        # Path = Obj.GetPath()
        # Suffix = os.path.basename(Path)
        # pathsplit = Path.split('.')
        # if len(pathsplit) > 1:
        #     Suffix = '.' + pathsplit[-1]
        # else:
        #     Suffix = ''
        # Obj = self.FindWindowByName('picker' + self.PathNames[1])
        # WildCard = 'All files(*' + Suffix + ')|*' + Suffix
        # Obj.SetWildcard(WildCard)
        pass

    def OnSave(self, event):
        self.Paths = []
        for index, name in enumerate(self.PathNames):
            obj = self.FindWindowByName('picker' + name)
            path = obj.GetPath()
            if index == 0:
                Suffix = os.path.basename(path)
                pathsplit = path.split('.')
                if len(pathsplit) > 1:
                    Suffix = '.' + pathsplit[-1]
                else:
                    Suffix = ''
                if not os.path.isfile(path):
                    wx.MessageBox('%s 模板文件不存在!' %
                                  path, 'oops!',
                                  style=wx.OK | wx.ICON_EXCLAMATION)
                    return
            else:
                if not os.path.exists(os.path.dirname(path)):
                    wx.MessageBox('%s 导出文件路径不存在!' %
                                  os.path.dirname(path), 'oops!',
                                  style=wx.OK | wx.ICON_EXCLAMATION)
                    return
                if path.split('.')[-1] != Suffix[1:]:
                    path += Suffix
            self.Paths.append(path)
        self.EndModal(retCode=1)

    def OnCancel(self, event):
        self.EndModal(retCode=5101)

    def ShowModal(self):
        ret = super().ShowModal()
        if ret == 5101:
            return ret
        elif ret == 1:
            return self.Paths
        else:
            return 5101


if __name__ == "__main__":
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))

    class MyFrame(wx.Frame):
        def __init__(self):
            super().__init__(None, -1, 'haha', size=(200, 100))
            btn = wx.Button(self, -1, 'baba', size=(10, 10))
            self.Bind(wx.EVT_BUTTON, self.OnClick, btn)

        def OnClick(self, event):
            # modal = DiaTable2add(self, 'baba').ShowModal()
            data = [1, 2, 34, 56]
            dia = DiaPickTemp(self, 'haha', path=BASE_DIR)
            modal = dia.ShowModal()
            print(modal)
            dia.Destroy()

    app = wx.App()
    MyFrame().Show()
    app.MainLoop()
